#include "http/znx_http_headers.h"
#include "comm/znx_log.h"

int main()
{
    znx_http_header_t   *header;
    znx_http_headers_t  headers;
    znx_pool_t *pool = znx_pool_create();    
    znx_http_headers_init(&headers);

    znx_str_t key, val;

    key.data = (u_char *)"aAAA"; key.len = znx_strlen(key.data);
    val.data = (u_char *)"1111"; val.len = znx_strlen(val.data);
    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    header->key = key; header->val = val;
    znx_http_headers_add(&headers, header);
    key.data = (u_char *)"AAAA"; key.len = znx_strlen(key.data);
    val.data = (u_char *)"2222"; val.len = znx_strlen(val.data);
    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    header->key = key; header->val = val;
    znx_http_headers_add(&headers, header);

    key.data = (u_char *)"BbBB"; key.len = znx_strlen(key.data);
    val.data = (u_char *)"1111"; val.len = znx_strlen(val.data);
    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    header->key = key; header->val = val;
    znx_http_headers_add(&headers, header);
    key.data = (u_char *)"BBBB"; key.len = znx_strlen(key.data);
    val.data = (u_char *)"2222"; val.len = znx_strlen(val.data);
    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    header->key = key; header->val = val;
    znx_http_headers_add(&headers, header);

    key.data = (u_char *)"CCcC"; key.len = znx_strlen(key.data);
    val.data = (u_char *)"1111"; val.len = znx_strlen(val.data);
    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    header->key = key; header->val = val;
    znx_http_headers_add(&headers, header);
    key.data = (u_char *)"cccc"; key.len = znx_strlen(key.data);
    val.data = (u_char *)"2222"; val.len = znx_strlen(val.data);
    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    header->key = key; header->val = val;
    znx_http_headers_add(&headers, header);

    key.data = (u_char *)"DDDD"; key.len = znx_strlen(key.data);
    val.data = (u_char *)"1111"; val.len = znx_strlen(val.data);
    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    header->key = key; header->val = val;
    znx_http_headers_add(&headers, header);
    key.data = (u_char *)"DDDD"; key.len = znx_strlen(key.data);
    val.data = (u_char *)"2222"; val.len = znx_strlen(val.data);
    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    header->key = key; header->val = val;
    znx_http_headers_add(&headers, header);

    key.data = (u_char *)"eeee"; key.len = znx_strlen(key.data);
    val.data = (u_char *)"1111"; val.len = znx_strlen(val.data);
    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    header->key = key; header->val = val;
    znx_http_headers_add(&headers, header);
    key.data = (u_char *)"EEEE"; key.len = znx_strlen(key.data);
    val.data = (u_char *)"2222"; val.len = znx_strlen(val.data);
    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    header->key = key; header->val = val;
    znx_http_headers_add(&headers, header);

    // get & get_next

    key.data = (u_char *)"aaaa"; key.len = znx_strlen(key.data);
    header = znx_http_headers_get(&headers, &key, 0);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"1111", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 1111, but got other");
    }
    header = znx_http_headers_get(&headers, &key, 1);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"2222", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 2222, but got other");
    }

    key.data = (u_char *)"bbbb"; key.len = znx_strlen(key.data);
    header = znx_http_headers_get(&headers, &key, 0);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"1111", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 1111, but got other");
    }
    header = znx_http_headers_get(&headers, &key, 1);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"2222", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 2222, but got other");
    }

    key.data = (u_char *)"cccc"; key.len = znx_strlen(key.data);
    header = znx_http_headers_get(&headers, &key, 0);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"1111", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 1111, but got other");
    }
    header = znx_http_headers_get(&headers, &key, 1);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"2222", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 2222, but got other");
    }

    key.data = (u_char *)"dddd"; key.len = znx_strlen(key.data);
    header = znx_http_headers_get(&headers, &key, 0);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"1111", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 1111, but got other");
    }
    header = znx_http_headers_get(&headers, &key, 1);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"2222", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 2222, but got other");
    }

    key.data = (u_char *)"eeee"; key.len = znx_strlen(key.data);
    header = znx_http_headers_get(&headers, &key, 0);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"1111", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 1111, but got other");
    }
    header = znx_http_headers_get(&headers, &key, 1);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"2222", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 2222, but got other");
    }


    // set
    key.data = (u_char *)"CCCC"; key.len = znx_strlen(key.data);
    val.data = (u_char *)"3333"; val.len = znx_strlen(val.data);
    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    header->key = key; header->val = val;
    znx_http_headers_set(&headers, header);

    // delete once
    key.data = (u_char *)"dddd"; key.len = znx_strlen(key.data);
    znx_http_headers_del_once(&headers, &key);

    // delete
    key.data = (u_char *)"bbbb"; key.len = znx_strlen(key.data);
    znx_http_headers_del(&headers, &key);

    // remove non-existent headers
    key.data = (u_char *)"ffff"; key.len = znx_strlen(key.data);
    znx_http_headers_del(&headers, &key);


    // get & get_next

    key.data = (u_char *)"aaaa"; key.len = znx_strlen(key.data);
    header = znx_http_headers_get(&headers, &key, 0);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"1111", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 1111, but got other");
    }
    header = znx_http_headers_get(&headers, &key, 1);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"2222", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 2222, but got other");
    }

    key.data = (u_char *)"bbbb"; key.len = znx_strlen(key.data);
    header = znx_http_headers_get(&headers, &key, 0);
    if (header != NULL)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect NULL, but got other");
    }

    key.data = (u_char *)"cccc"; key.len = znx_strlen(key.data);
    header = znx_http_headers_get(&headers, &key, 0);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"3333", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 3333, but got other");
    }
    header = znx_http_headers_get(&headers, &key, 1);
    if (header != NULL)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect NULL, but got other");
    }

    key.data = (u_char *)"dddd"; key.len = znx_strlen(key.data);
    header = znx_http_headers_get(&headers, &key, 0);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"2222", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 2222, but got other");
    }
    header = znx_http_headers_get(&headers, &key, 1);
    if (header != NULL)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect NULL, but got other");
    }

    key.data = (u_char *)"eeee"; key.len = znx_strlen(key.data);
    header = znx_http_headers_get(&headers, &key, 0);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"1111", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 1111, but got other");
    }
    header = znx_http_headers_get(&headers, &key, 1);
    if (header == NULL ||
        znx_strncmp(header->val.data, (u_char *)"2222", header->val.len) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect 2222, but got other");
    }

    znx_pool_destroy(pool);

    return 0;
}